package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neusoft.EYSystem.dao.CaretakerClientDAO;
import com.neusoft.EYSystem.dao.ClientDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.CaretakerClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/caretakerClient")
public class CaretakerClientController {
    @Autowired
    private CaretakerClientService caretakerClientService;
    @Autowired
    private CaretakerClientDAO caretakerClientDAO;
    // 管理员端 护工管理 设置服务对象 按钮根据护工id 显示其管理的客户列表（下表）
    // 查询某健康管家已包含的客户,已包含中再查询
    @Autowired
    private ClientDAO clientDAO;

    @GetMapping("/getIncludedClients")
    public PageResponseBean<List<Client>> getIncludedClients(Long cur, Long number, Client client, User user) {
        return caretakerClientService.getIncludedClients(cur, number, client, user);
    }

    // 管理员端 护工管理 设置服务对象 显示所有没有健康管家的客户列表（上表）
    // 查询无健康管家的客户,再查询
    @GetMapping("/caretakerClient")
    public PageResponseBean<List<Client>> getExcludedProjects(Long cur, Long number,Client client, User user) {
        return caretakerClientService.getExcludedClients(cur, number, client, user);
    }

    // 管理员端 护工管理 设置服务对象 按钮根据护工id和客户id
    // 添加客户到护工
    @PostMapping("/addClientToUser")
    public ResponseBean<Integer> addClientToUser(Client client, User user) {
        ResponseBean<Integer> response = new ResponseBean<>();
        CaretakerClient caretakerClient = new CaretakerClient();
        caretakerClient.setCaretakerId(user.getUserId());
        caretakerClient.setClientId(client.getClientId());
        caretakerClient.setIsServered(true); // 默认服务

        try {
            if(caretakerClientDAO.insert(caretakerClient) > 0){
                Client client1 = clientDAO.selectById(client.getClientId());
                client1.setUserId(user.getUserId());
                if(clientDAO.updateById(client1) > 0){
                    response.setStatus(200);
                    response.setMsg("护工添加客户成功");
                    return response;
                }
            }else {
                response.setStatus(400);
                response.setMsg("护工添加客户失败，可能是数据不完整或重复");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("护工添加客户失败: " + e.getMessage());
        }
        return response;
    }

    // 管理员端 护工管理 设置服务对象 按钮根据护工id和客户id
    // 从护工移除客户
    @PostMapping("/removeClientFromUser")
    public ResponseBean<Integer> removeClientFromUser(Client client, User user) {
        return caretakerClientService.removeClientFromUser(client, user);
    }
}
